<?php
//=========================
//Vuong Van Binh
//Wanbin@vietnambiz.com
//Faq Functions 
//=========================

//ACTION ADD NEW CAT
function add_new_faq_category(){

  global $CONFIG,$db;
  
  if($_POST["add_new_cat"]){     
	  $cat_name 	 = strip_tags($_POST["cat_name"]);
	  
	  $cat_name_link = create_link($cat_name);
   
      
      
	  if(cat_name_is_exist($cat_name_link)){
	   
		  $cat_name_link = $cat_name_link."-".rand(1,999);
	   
	   }
	  
	  if(empty($cat_name)){
	   
	     $CONFIG["error_message"] = "<li>Category Name Not Empty.</li>";
	   
	  }
	  
	  if(empty($CONFIG["error_message"])){
	      
		  $data = array();
		  
		  $data["cat_name"] 		= $cat_name;
		  $data["cat_name_link"] 	= $cat_name_link;
		  
		  $db->exec_insert("cat_faq",$data); 

		  
	      if(mysql_affected_rows()>0){
			  $CONFIG["success_message"] .= "<li>Add New Category Successfully.</li>";
		  }
	  }

  }

}
//ACTION EIT CAT
function edit_faq_category(){

  global $CONFIG,$db;
  
  if($_POST["edit_cat"]){     
  
	  $cat_name 	 = strip_tags($_POST["cat_name"]);
	  $cat_edit_id   = intval($_POST["cat_edit_id"]);
	  
	  if(empty($cat_name)){
	   
	     $CONFIG["error_message"] = "<li>Category Name Not Empty.</li>";
	   
	  }
	  
	  if(empty($CONFIG["error_message"])){
	      
		  $data = array();
		  
		  $data["cat_name"] = $cat_name;
		  
		  $db->exec_update("cat_faq",$data,'`cat_id`='.$cat_edit_id); 

		  
	      if(mysql_affected_rows()>0){
			  $CONFIG["success_message"] .= "<li>Edit Category Successfully.</li>";
		  }
	  }

  }

}
//ACTION DELETE CAT FAQ
function del_cat_faq(){
  
    global $db;

	$db->exec_delete('cat_faq',"`cat_id`=".intval($_GET["del"]));

}

//ACTION DEL MULTI CAT
function del_multi_cat(){
  
  global $db;
  
  if($_POST["del_multi"]){
  
  	   $arr_id = $_POST["check"];
       foreach($arr_id as $i=>$id){
	      
	   		$db->exec_delete('cat_faq',"`cat_id`=".intval($id));
		 
	   }
	
  }

}
//ACTION DELETE FAQ
function del_faq(){
  
    global $db;

	$db->exec_delete('faq',"`id`=".intval($_GET["del"]));

}

//ACTION DEL MULTI FAQ
function del_multi_faq(){
  
  global $db;
  
  if($_POST["del_multi"]){
  
  	   $arr_id = $_POST["check"];
       foreach($arr_id as $i=>$id){
	      
	   		$db->exec_delete('faq',"`id`=".intval($id));
		 
	   }
	
  }

}
//GET All CAT FAQ
function get_all_cat_faq(){

    global $db;
	
    $sql = "SELECT * FROM cat_faq ORDER BY cat_id DESC";
	
	$qr=$db->query($sql);
	
	return $qr;
  
}
//GET CAT FAQ TO EDIT
function get_cat_faq_edit(){

  global $db;
  
  if($_GET["edit"]){
  
	  $edit_id = intval($_GET["edit"]);
	
	  $cat_edit = $db->query_first("SELECT * FROM cat_faq WHERE cat_id =".$edit_id);
	  
	  if(!$edit_id){
	  
		redirect(ADMIN_URL);
		
	  }
	  
	  return $cat_edit;
  }
}
//GET FAQ TO EDIT
function get_faq_edit(){

  global $db;
  
  if($_GET["id"]){
  
	  $edit_id = intval($_GET["id"]);
	
	  $faq_edit = $db->query_first("SELECT * FROM faq WHERE id =".$edit_id);
	  
	  if(!$edit_id){
	  
		redirect(ADMIN_URL);
		
	  }
	  
	  return $faq_edit;
  }
}
//ACTION ADD NEW FAQ
function add_new_faq(){

  global $CONFIG,$db;
  
  if($_POST["add_new_faq"]){
      
	  $title 		= strip_tags($_POST["title"]);
	  $cat_id 		= intval($_POST["cat"]);
	  $content   	= $_POST["content"];
	  
	  $title_link = create_link($title);
	  
	  if(faq_title_is_exist($title_link)){
	   
		  $title_link = $title_link."-".rand(1,999);
	   
	   }
	  
	  if(empty($title)){
	   
	     $CONFIG["error_message"] = "<li>Title Not empty.</li>";
	   
	  }
	  if(empty($content)){
	   
	     $CONFIG["error_message"] .= "<li>Content Not Empty.</li>";
	   
	  }
	  
	  
	  if(empty($CONFIG["error_message"])){
	      
		  $data = array();
		  
		  $data["title"] 		= $title;
		  $data["title_link"] 	= $title_link;
	      $data["cat_id"] 		= $cat_id;
		  $data["content"] 		= $content;
		  
		  $db->exec_insert("faq",$data);
		  
		  if(mysql_affected_rows()>0){
		  
		  		if($_POST["exit"]){

					server_redirect(ADMIN_URL."/index.php?module=faq");
				
				}
				
				$CONFIG["success_message"] .= "<li>Add New FAQ successfully.</li>";
				
				unset($_POST);
		  
		  }
		 
	  }

	
  }

}
//ACTION EDIT FAQ
function edit_faq(){

  global $CONFIG,$db;
  
  if($_POST["edit_faq"]){
      
	  $title 		= strip_tags($_POST["title"]);
	  $cat_id 		= intval($_POST["cat"]);
	  $content   	= $_POST["content"];
	  
	  $edit_id		= intval($_POST["edit_id"]);
	  
	  if(empty($title)){
	   
	     $CONFIG["error_message"] = "<li>Title Not empty.</li>";
	   
	  }
	  if(empty($content)){
	   
	     $CONFIG["error_message"] .= "<li>Content Not Empty.</li>";
	   
	  }
	  
	  
	  if(empty($CONFIG["error_message"])){
	      
		  $data = array();
		  
		  $data["title"] 		= $title;
	      $data["cat_id"] 		= $cat_id;
		  $data["content"] 		= $content;
		  
		  $db->exec_update("faq",$data,'`id`='.$edit_id);
		  
		  if(mysql_affected_rows()>0){
		  
		  		if($_POST["exit"]){

					server_redirect(ADMIN_URL."/index.php?module=faq");
				
				}
		  		
				$CONFIG["success_message"] .= "<li>Edit FAQ successfully.</li>";
				
				unset($_POST);
		  
		  }
		 
	  }

	
  }

}
//GET ALL FAQ PAGE
function get_all_faq_page()
{

	global $db,$CONFIG;

	$cat_id = intval($_GET["cat"]);

	if($cat_id>0){
		
		$sel_all=$db->query_first("SELECT COUNT(id) m FROM faq WHERE cat_id = ".$cat_id);
		
		$sql = "SELECT faq.*,cat_faq.cat_name FROM faq,cat_faq WHERE faq.cat_id = cat_faq.cat_id AND faq.cat_id = ".$cat_id." ORDER BY id DESC";
	
	}else{
	
		$sel_all=$db->query_first("SELECT COUNT(id) m FROM faq");
		
		$sql = "SELECT faq.*,cat_faq.cat_name FROM faq,cat_faq WHERE faq.cat_id = cat_faq.cat_id ORDER BY id DESC";
		
	
	}
	
	$page=intval($_GET["page"]) ? intval($_GET["page"]) :1;
	$page=intval($page);
	if ($page<0) $page=1;
	$number=$sel_all['m']; 
	$limit = $CONFIG['rows_per_page_faq'];
	$total=ceil($number/$limit); 
	$start = (($page*$limit)-$limit);
	
	$faq = $db->query($sql." LIMIT ".$start.",".$limit);
	//Xu ly ban ghi cuoi cung thi sap xep lai trang tien den trang ke tiep
	if($page >1 &&$db->num_rows($faq)<1)
	{
	   $page = $page -1;
	   
	   $total=ceil($number/$limit); 
	   $start = (($page*$limit)-$limit);
	
	   $faq = $db->query($sql." LIMIT ".$start.",".$limit);
	   
	   sever_redirect(get_string_url()."&page=".$page);
	}
	
	return array("faq"=>$faq,"page"=>$page,"total"=>$total);

}
function cat_name_is_exist($name)
{
    global $db;
	
	$result = $db->query_first("SELECT cat_name_link FROM cat_faq WHERE cat_name_link = '".$name."'");
     
	if($result) {
		return true;
	} else {
		return false;
	}

}
function faq_title_is_exist($name)
{
    global $db;
	
	$result = $db->query_first("SELECT title_link FROM faq WHERE title_link = '".$name."'");
     
	if($result) {
		return true;
	} else {
		return false;
	}

}
?>